package cn.itsource.controller.remote;

import cn.itsource.api.RuleStartRemoteApi;
import cn.itsource.constants.RConst;
import cn.itsource.dto.RuleDto;
import cn.itsource.mapper.*;
import cn.itsource.pojo.domain.ChargeRuleReturn;
import cn.itsource.pojo.domain.ChargeRuleStart;
import cn.itsource.pojo.domain.ChargeRuleWait;
import cn.itsource.result.JSONResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@RestController
public class RuleRemoteApiImpl implements RuleStartRemoteApi {
    @Autowired
    private ChargeRuleStartMapper chargeRuleStartMapper;
    @Autowired
    private ChargeRuleReturnMapper chargeRuleReturnMapper;
    @Autowired
    private ChargeRuleWaitMapper chargeRuleWaitMapper;

    @Autowired
    private ProfitsharingRuleBaseMapper profitsharingRuleBaseMapper;

    @Autowired
    private ProfitsharingRuleDeductCancelMapper profitsharingRuleDeductCancelMapper;
    @Autowired
    private ProfitsharingRuleDeductComplaintMapper profitsharingRuleDeductComplaintMapper;
/*
    @Autowired
    private RuleStartRemoteApi RuleStartRemoteApi;
*/

    @Override
    public JSONResult valuation(String mileage){

        int hour = LocalDateTime.now().getHour();

        //得到规则
        ChargeRuleStart chargeRuleStart = chargeRuleStartMapper.selectOne(new LambdaQueryWrapper<ChargeRuleStart>()
                .lt(ChargeRuleStart::getHourStart, hour)
                .ge(ChargeRuleStart::getHourEnd, hour));
        //得到起步价
        BigDecimal baseMileage = chargeRuleStart.getBaseMileage();
        //如果返回值为负数，表示第一个对象小于第二个对象；
        //如果返回值为零，则两个对象相等；
        //如果返回值为正数，则表示第一个对象大于第二个对象
        BigDecimal mileageBig = new BigDecimal(mileage);
        int i = baseMileage.compareTo(mileageBig);
        if(i >= RConst.Num.INT_0){
            return JSONResult.success(chargeRuleStart.getAmount());
        }else {
            //多余的公里数
            BigDecimal subtract = mileageBig.subtract(baseMileage);
            //多出来的钱
            BigDecimal multiply = subtract.multiply(chargeRuleStart.getExceedEveryKmAmount());
            BigDecimal total = multiply
                    .add(chargeRuleStart.getAmount())//多余公里数的钱
                    .add(returnJourney(mileageBig));//返程的钱
            return JSONResult.success(total);
        }
    }
    /**
     * 计算返程费用
     * @param mileageBig
     * @return
     */
    public BigDecimal returnJourney(BigDecimal mileageBig){
        ChargeRuleReturn chargeRuleReturn = chargeRuleReturnMapper.selectById(RConst.Num.BASE_LONG_1);
        BigDecimal freeBaseReturnMileage = chargeRuleReturn.getFreeBaseReturnMileage();
        int i = freeBaseReturnMileage.compareTo(mileageBig);
        if(i >= RConst.Num.INT_0){
            return new BigDecimal(RConst.Num.INT_0);
        }else {
            BigDecimal subtract = mileageBig.subtract(freeBaseReturnMileage);
            BigDecimal multiply = subtract.multiply(chargeRuleReturn.getExceedEveryKmAmount());
            return multiply;
        }
    }

    @Override
    public JSONResult getRuleDto(Integer hours) {
        RuleDto ruleDto = new RuleDto();
        //开始里程
        ChargeRuleStart chargeRuleStart = chargeRuleStartMapper.selectOne(new LambdaQueryWrapper<ChargeRuleStart>()
                .lt(ChargeRuleStart::getHourStart, hours)
                .ge(ChargeRuleStart::getHourEnd, hours));
        ruleDto.setBaseMileage(chargeRuleStart.getBaseMileage());
        ruleDto.setBaseMileageAmount(chargeRuleStart.getAmount());
        ruleDto.setExceedBaseMileageEveryKmAmount(chargeRuleStart.getExceedEveryKmAmount());

        ChargeRuleReturn chargeRuleReturn = chargeRuleReturnMapper.selectById(RConst.Num.BASE_LONG_1);
        ruleDto.setFreeBaseReturnMileage(chargeRuleReturn.getFreeBaseReturnMileage());
        ruleDto.setExceedBaseReturnEveryKmAmount(chargeRuleReturn.getExceedEveryKmAmount());

        ChargeRuleWait chargeRuleWait = chargeRuleWaitMapper.selectById(RConst.Num.BASE_LONG_1);
        ruleDto.setFreeBaseWaitingMinute(chargeRuleWait.getFreeBaseWaitingMinute());
        ruleDto.setExeceedBaseWaitingEveryKmAmount(chargeRuleWait.getExceedEveryMinuteAmount());
        return JSONResult.success(ruleDto);
    }
}
